/*----------------------------------------------
| noka select v1.0 www.97521.com                 |
| rebin 2014-01-15                             |
|---------------------------------------------*/
var nselectbutton = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.cfg = cfg;					//配置信息
    	this.id=cfg.id; //id
    	this.name=cfg.name;//name
    	this.rurl=cfg.rurl;
    	this.width=cfg.width;//宽
    	this.bheight=cfg.bheight;//弹出框高
    	this.bwidth=cfg.bwidth;//弹出框宽
    	this.value = cfg.value;
    	this.svalue = cfg.svalue;
    	this.sdy = $(cfg.id+'_sDiv');
    	this.readonly = ('readonly'==cfg.readonly);
    	this.disabled = ('disabled'==cfg.disabled);
    	this.allownull = cfg.allownull;//为true表示可以为空
    	this.placeholder = cfg.placeholder;
    	this.lotype = cfg.lotype;
    	this.openfunction= cfg.openfunction;
    	this.closefunction = cfg.closefunction;
    	this.msg = cfg.msg || '';
    	this.hImg;
    	this.mImg;
    },
    show : function(){
    	this.PreLoding();
    	this.initselect();
    	this.initObjCht();
    	this.selectEvent();
    	this.ExEvents();
    },
    //-------------------初始化下拉框-------------------
    initselect : function(){
    	var idx = 0;
        var html = [];
        var self = this;
        var bodyHtml=$(self.id+'_selectDiv');
        self.width-=18;
        //------------配置初始化--------------------------
        html[idx++] = '<div id="'+self.id+'_sidiv" class="selectbutton-dropdownchecklist nk_input_lineheight nk_input_sdDiv" style="width:'+self.width+'px;float:left;">';
        html[idx++] = '<input id="'+self.id+'"   name="'+self.name+'" type="hidden" value="'+self.value+'">';
        html[idx++] = '<input id="'+self.id+'_sinput" placeholder="'+self.placeholder+'" class="nk_input_lineheight nk_select_input_text" type="text" style="width:'+self.width+'px;" readonly="readonly" '+(self.disabled?'disabled="disabled"':'')+' value="'+self.svalue+'">';
        html[idx++] = '</div>';
        if(!self.allownull){
        	html[idx++] = '<div style="float:left;color: red;" class="nk_input_imgheight">*</div>';
        	html[idx++] = '<div style="float:left;"><img style="display:none;vertical-align:middle" class="nk_input_imgheight" id="'+self.id+'_img" src="'+self.cfg.rurl+'/nokatag/formcheck/images/exclamation.gif"></img></div>';
        }
        bodyHtml.innerHTML=html.join('');
    },
    //--------初始化验证-----------------
    initObjCht : function(){
    	var self = this;
    	$(self.id).veri = function(){
    	    if(($(self.id).value.trim().length<1) && !self.allownull){//不能为空
    	    	$(self.id+'_img').show();
    	    	return false;
    	    }
    	    try{$(self.id+'_img').hide();}catch(e){}
    	    return true;
    	};
    },
    //----------------外部事件---------------------
    ExEvents : function(){
    	var self = this;
    	var textObj = $(self.id+'_sinput');
        var valueObj =$(self.id);
    	//------------设置值------------------
    	$(self.id).setValue = function(v,sv){
    		valueObj.value = v;
    		textObj.value=sv;
            try{$(self.id+'_img').hide();}catch(e){}
    	};
    	$(self.id).hide = function(){
    		 try{NokaBox.box.hide();}catch(e){}
    	};
    	//----------------------------------
    	$(self.id).setDisabled = function(a){
    		$(self.id+'_sinput').writeAttribute({disabled:a});
    		self.disabled=a;
    	};
    	$(self.id).setReadonly = function(a){
    		$(self.id+'_sinput').writeAttribute({readonly:a});
    		self.readonly=a;
    	};
    },
    urlParesId : function(binfo){
    	var self = this;
    	if(binfo.indexOf('?')==-1){
			binfo +='?nid='+self.id;
		}else{
			binfo +='&nid='+self.id;
		}
    	return binfo;
    },
    //===================下拉框事件============================================================
    selectEvent : function(){
    	var self = this;
        var obj = $(self.id+'_sidiv');
	    obj.observe('click',function(a){
	    	if(self.readonly || self.disabled){
	    		return;
	    	}
	    	var binfo = $(self.id+'_BoxDiv').innerHTML;
	    	if('iframe'==self.lotype){
	    		binfo=self.urlParesId(binfo);
	    		NokaBox.box.show({iframe:binfo,boxid:self.id+'_boxid',fixed:false,width:self.bwidth,height:self.bheight,openjs:self.openfunction,closejs:self.closefunction});
	    	}else if('html'==self.lotype)
	    		NokaBox.box.show({html:binfo,boxid:self.id+'_boxid',fixed:false,width:self.bwidth,height:self.bheight,openjs:self.openfunction,closejs:self.closefunction});
	    	else if('url'==self.lotype){
	    		binfo=self.urlParesId(binfo);
	    		NokaBox.box.show({url:binfo,boxid:self.id+'_boxid',fixed:false,width:self.bwidth,height:self.bheight,openjs:self.openfunction,closejs:self.closefunction});
	    	}else if('image'==self.lotype)
	    		NokaBox.box.show({image:binfo,boxid:self.id+'_boxid',fixed:false,width:self.bwidth,height:self.bheight,openjs:self.openfunction,closejs:self.closefunction});
	    });
    	obj.observe('mousemove',function(){
     		self.setSelectStyle(obj);
     	});
        obj.observe('mouseout',function(event){
         	var element = event.element();
         	setTimeout(function(){
                 self.setNoSelectStyle(obj);
             }, 500);
         });
        document.observe('widget:noka_reinit',function(event){//失失焦点时验证其值
        	if(self.id.indexOf('_nkADinsert_')==-1){
     	    	var npfffs=event.memo.widgetNumber.clone();
     	    	for(var i=0;i<npfffs.length;i++){
     	    		var cfgid = Object.allclone(self.cfg);
     	    		var initem = npfffs[i];
	    			cfgid.id = initem.id;
     	    		if(cfgid.id.indexOf('_'+self.id+'_')!=-1){
     	    			(new nselectbutton(cfgid)).show();
     	    			try{
	    					var idx = cfgid.id.substring(cfgid.id.lastIndexOf('_')+1,cfgid.id.length);
	    					$(cfgid.id).name=$(cfgid.id).name.replace(/\[0\]/g,'['+idx+']');
	    					if(undefined!=initem.value && undefined!=initem.svalue && $(cfgid.id).name==initem.name){
	    						$(cfgid.id).setValue(initem.value,initem.svalue);
	    					}
	    				}catch(e){}
     	    		}
     	    	}
        	}
        });
    },
   //----------选种状态-----------------------
    setSelectStyle : function(obj){
    	var self = this;
    	obj.setStyle({borderColor:'#5794bf',backgroundImage:'url('+self.hImg.src+')'});
    },
    //-----------未选种状态-------------------
    setNoSelectStyle : function(obj){
    	var self = this;
    	obj.setStyle({borderColor:'#AFAFAF',backgroundImage:'url('+self.mImg.src+')'});
    },
    //-----------预加载图片-------------------
    PreLoding : function(){
    	var self = this;
    	self.hImg = new Image(); 
    	self.mImg = new Image();
    	self.hImg.src=self.cfg.rurl+'/nokatag/org/noka/select/dropdown_hoverfd.png';
    	self.mImg.src=self.cfg.rurl+'/nokatag/org/noka/select/dropdownfd.png';
    	NBackgroundImageCache();
    }
});